import axios from 'axios'
import router from '../router/index'

const isDev = process.env.NODE_ENV === 'development'
// console.log(isDev)
const request = axios.create({
  baseURL: isDev ? '' : 'http://121.89.205.189/api',
  timeout: 6000
})

request.interceptors.request.use((config) => {
  // console.log(config)
  config.headers.common.token = localStorage.getItem('token') || ''
  return config
}, (error) => {
  return Promise.reject(error)
})

request.interceptors.response.use((response) => {
  // console.log(response)
  if (response.data.code === '10119') { // token失效 ***********************
    // 没有传递token 传了错误的token  传了正确的token但是过期了
    router.push('/login')
  }
  return response
}, (error) => {
  return Promise.reject(error)
})

export default request
